/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_08_Introspection.java]
 * Creation Date        [08-12-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_reflection;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * Java的内省机制
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_08_Introspection {

	/**
	 * 测试方法
	 * 
	 * @param args
	 * @throws IntrospectionException
	 */
	public static void main(String[] args) throws IntrospectionException {
		// 获取JavaBean的Information, 第一个参数是要内省的JavaBean, 第二个参数为要排除分析的父类
		BeanInfo beanInfo = Introspector.getBeanInfo(SimpleBean.class,
				Object.class);
		PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor propertyDescriptor : descriptors) {
			String propName = propertyDescriptor.getDisplayName();
			String propType = propertyDescriptor.getPropertyType()
					.getSimpleName();
			System.out.println(propType + ' ' + propName);
			Method readMethod = propertyDescriptor.getReadMethod();
			Method writeMethod = propertyDescriptor.getWriteMethod();
			System.out.println(readMethod.getName());
			System.out.println(writeMethod.getName());
		}

		System.out.println();
		PropertyDescriptor pd = new PropertyDescriptor("name", SimpleBean.class);
		System.out.println(pd.getName());
	}
}

/**
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
class SimpleBean {

	private String name;

	private String password;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
